﻿using Microsoft.Extensions.Options;
using RuoVea.ExEnum;
using RuoVea.ExSugar;

namespace RuoVea.CodeGen.Web.Controllers;
/// <summary>
/// 
/// </summary>
public partial class DbConnStrInfoController : Controller
{
    /// <summary>
    /// 
    /// </summary>
    private readonly SugarRepository<GenDbConnString> _thisRepository;
    private readonly List<DbConnectionConfig> _dbConnectionConfigs;
    private readonly ISqlSugarClient _defultDbContext;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="thisRepository"></param> 
    /// <param name="dbConnectionConfigs"></param> 
    public DbConnStrInfoController(SugarRepository<GenDbConnString> thisRepository,  IOptions<List<DbConnectionConfig>> dbConnectionConfigs, ISqlSugarClient defultDbContext)
    {
        _thisRepository = thisRepository;
        _dbConnectionConfigs = dbConnectionConfigs.Value;
        _defultDbContext = defultDbContext;
    }

    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("/DbConnStrInfo/pages")]
    public async Task<RestfulResult<PageResult<GenDbConnString>>> GetPagesAsync()
    {
        var restful = new RestfulResult<PageResult<GenDbConnString>>() { Code = CodeStatus.OK };

        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception("未提供上传数据。请在Request.Body中携带上传数据。");
        PageParam? data = JsonConvert.DeserializeObject<PageParam>(dataStr);
        data ??= new PageParam { PageNo = 1, PageSize = 10 };

        var query = await _thisRepository.AsQueryable()
            .ToPageAsync(data.PageNo, data.PageSize);

        List<GenDbConnString> dbConnStrInfos = query.Rows.ToList();
        _dbConnectionConfigs.ForEach(x =>
        {
            dbConnStrInfos.Add(new GenDbConnString { Id = 0, ConnectionStrings = x.ConnectionString, DbName = "系统配置", DbType = (int)x.DbType, IsSys = 1 });
        });
        query.Rows = dbConnStrInfos;
        restful.Data = query;
        return restful;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpGet("/DbConnStrInfo/list")]
    public async Task<RestfulResult<List<GenDbConnString>>> GetlistAsync()
    {
        var restful = new RestfulResult<List<GenDbConnString>>() { Code = CodeStatus.OK };
       
        var query = await _thisRepository.AsQueryable()
            .ToListAsync();
        restful.Data = query;

        List<GenDbConnString> dbConnStrInfos = restful.Data;
        _dbConnectionConfigs.ForEach(x =>
        {
            dbConnStrInfos.Add(new GenDbConnString { Id = 0, ConnectionStrings = x.ConnectionString, DbName = "系统配置", DbType = (int)x.DbType, IsSys = 1 });
        });
        restful.Data = dbConnStrInfos;
        return restful;
    }
    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpGet("/DbConnStrInfo/Info")]
    public async Task<RestfulResult<PageResult<GenDbConnString>>> GetInfoAsync(long id)
    {
        var restful = new RestfulResult<PageResult<GenDbConnString>>() { Code = CodeStatus.OK };
        PageParam data = new PageParam { PageNo = 1, PageSize = 100 };
        var query = await _thisRepository.AsQueryable()
            .ToPageAsync(data.PageNo, data.PageSize);
        restful.Data = query;

        List<GenDbConnString> dbConnStrInfos = restful.Data.Rows.ToList();
        _dbConnectionConfigs.ForEach(x =>
        {
            dbConnStrInfos.Add(new GenDbConnString { Id = 1, ConnectionStrings = x.ConnectionString, DbName = "系统配置", DbType = (int)x.DbType, IsSys = 1 });
        });
        restful.Data.Rows = dbConnStrInfos;
        return restful;
    }
    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("/DbConnStrInfo/add")]
    public async Task<RestfulResult> AddAsync([FromBody] GenDbConnString data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        bool result = await _thisRepository.InsertAsync(data);
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "添加失败";
            return restful;
        }
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("/DbConnStrInfo/update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] GenDbConnString data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };
        bool result = await _thisRepository.UpdateAsync(data, true, true, "笔记修改");
        if (result)
        {
            restful.Data = result;
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Message = "修改失败";
            return restful;
        }
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("/DbConnStrInfo/delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        RestfulResult restful = new() { Code = CodeStatus.BadRequest };

        if (data.Id == 0)
        {
            restful.Message = "删除失败";
            return restful;
        }
        bool isDelete = await _thisRepository.DeleteByIdAsync(data.Id);
        if (isDelete)
        {
            restful.Data = isDelete;
            restful.Message = "删除成功";
            restful.Code = CodeStatus.OK;
            return restful;
        }
        else
        {
            restful.Data = isDelete;
            restful.Message = "删除失败";
            return restful;
        }
    }
}